import axios from "axios";

const configBaseUrl = 'http://127.0.0.1:8000';
const codeHandlerMap = new Map<number, Function>();

codeHandlerMap.set(2000, (response: JSON) => getJSONConfig(response).data.data);
codeHandlerMap.set(4000, (response: JSON) => { throw getJSONConfig(response).data.msg; });

const getReponseData = async (res: JSON) => {
    const response = getJSONConfig(res);
    const handler = codeHandlerMap.get(response.data.code);
    return handler ? handler(response) : (() => { throw '未识别的响应代码'; })();
}

// TODO 具体 api 列表

/**
 * @param username 用户名
 * @param password 密码
 * @returns 用户名登录
 */
export const getUserLoginForUserPwdAPI = async (username: string, password: string) => {
    const res = await axios.post(`${configBaseUrl}/api/login/`, { captcha: '', password: password, username: username });
    const data = await getReponseData(getJSONConfig(res));
    return data;
}

/**
 * @param phone 手机号
 * @returns 得到手机验证码
 */
export const getUserLoginForGetPhoneCodeAPI = async (phone: string) => {
    console.log(phone);
    return '得到用户手机验证码';
}

/**
 * @param phone 手机号
 * @param phoneCode 得到手机验证码
 * @returns 手机号手机号验证码登录
 */
export const getUserLoginForPhoneLoginCodeAPI = async (phone: string, phoneCode: string) => {
    console.log(phone, phoneCode);
    return '用户手机验证码登录';
}

/**
 * @param userToken 用户的token 
 * @returns 得到用户的token
 */
export const getUserCardForUserInfo = async () => {
    asyncGetCookie('userLoginTooken').then((cookie) => {
        console.log("🚀 ~ asyncGetCookie ~ cookie:", cookie)
    })
}

/**
 * @returns 用户退出登录
 */
export const getUserLogoutForUserInfo = async () => {
    return '用户退出登录';
}